//
//  MyMovieViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/15.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MyMovieViewController.h"
#import "DataBaseSingleton.h"
#import "Movie.h"
#import "MovieDetailViewController.h"

@interface MyMovieViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *allArray;

@end

@implementation MyMovieViewController

- (void)dealloc{
    [_allArray release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     self.navigationItem.title = @"我的电影";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.allArray = [NSMutableArray array];
    self.allArray = [[DataBaseSingleton shareDataBase] selectMovie];
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:_myTableView];
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [_myTableView release];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    Movie *movie = [self.allArray objectAtIndex:indexPath.row];
    cell.textLabel.text = movie.movieName;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetail = [[MovieDetailViewController alloc] init];
    Movie *movie = [self.allArray objectAtIndex:indexPath.row];
    movieDetail.movieId = movie.movieId;
    NSLog(@"%@", movieDetail.movieId);
    [self.navigationController pushViewController:movieDetail animated:YES];
    

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return YES;
}

//- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
//    
//    [super setEditing:editing animated:animated];
//    
//    // tableView 自带的编辑方法
//    [self.myTableView setEditing:editing animated:animated];
//}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    self.allArray = [[DataBaseSingleton shareDataBase] selectMovie];
    Movie *movie = [self.allArray objectAtIndex:indexPath.row];
    [[DataBaseSingleton shareDataBase] deleteTable:movie.movieName];
    
    [self.allArray removeObjectAtIndex:indexPath.row];
    
    
    NSLog(@"movie = %@", movie.movieName);
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
